package com.cqw.robot.utils;

import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;

public class ScreenCaptureExample {
    static {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }


    /**
     * 全屏截图
     * @return 返回BufferedImage
     */
    public static BufferedImage getFullScreenShot() {
        BufferedImage bfImage = null;
        int width = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
        int height = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        try {
            Robot robot = new Robot();
            bfImage = robot.createScreenCapture(new Rectangle(0, 0, width, height));
        } catch (AWTException e) {
            e.printStackTrace();
        }
        return bfImage;
    }

    public static void main(String[] args) {

        BufferedImage screenFullImage = getFullScreenShot();

        // 截取一个区域，假设截取的区域是屏幕的一半
        int width = screenFullImage.getWidth();
        int height = screenFullImage.getHeight() / 2;
        BufferedImage screenPartImage = screenFullImage.getSubimage(0, 0, width, height);

        // 将BufferedImage转换为Mat
        Mat img = new Mat(screenPartImage.getHeight(), screenPartImage.getWidth(), CvType.CV_8UC3);


    }
}